//
//  LOTableViewCell.m
//  LegalObserver
//
//  Created by Roxanne Brittain on 10/30/11.
//  Copyright (c) 2011 Digifit. All rights reserved.
//

#import "LOTableViewCell.h"
#import "LOAppDelegate.h"

#define kTextFieldFrame CGRectMake(110, 11, 190, 24)
#define kTextViewFrame CGRectMake(0, 0, 290, 90)
#define kTextLabelFrame CGRectMake(0, 11, 100, 24)

@implementation LOTableViewCell
@synthesize titleLabel, textField, textView, key, object, showTime;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle: (style>7 ? UITableViewCellStyleDefault : style) reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        
        cellStyle = style;
        self.selectionStyle = UITableViewCellSelectionStyleGray;
        
        if (style == TABLE_CELL_STYLE_FORM) {
            titleLabel = [UILabel new];
            titleLabel.frame = kTextLabelFrame;
            titleLabel.textAlignment = UITextAlignmentRight;
            titleLabel.backgroundColor = [UIColor clearColor];
            titleLabel.font = [UIFont boldSystemFontOfSize:16];
            titleLabel.textColor = [UIColor darkTextColor];
            [self.contentView addSubview:titleLabel];
            
            textField = [UITextField new];
            textField.frame = kTextFieldFrame;
            textField.borderStyle = UITextBorderStyleNone;
            textField.delegate = self;
            textField.returnKeyType = UIReturnKeyDone;
            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            [self.contentView addSubview:textField];
            
            self.selectionStyle = UITableViewCellSelectionStyleNone;
        } else if (style == TABLE_CELL_STYLE_PERSON) {
            
        } else if (style == TABLE_CELL_STYLE_NOTE) {
            textView = [UITextView new];
            textView.frame = kTextViewFrame;
            textView.delegate = self;
            textView.returnKeyType = UIReturnKeyDone;
            textView.backgroundColor = [UIColor clearColor];
            textView.font = [UIFont systemFontOfSize:14];
            [self.contentView addSubview:textView];
            
            self.selectionStyle = UITableViewCellSelectionStyleNone;
        } else if (style == TABLE_CELL_STYLE_BUTTON) {
            self.textLabel.textAlignment = UITextAlignmentCenter;
            self.backgroundColor = [UIColor redColor];
            self.textLabel.textColor = [UIColor whiteColor];
            self.textLabel.shadowColor = [UIColor darkGrayColor];
            self.textLabel.shadowOffset = CGSizeMake(0, -.5);
        }
    }
    return self;
}

-(void)didSelectCell
{
    if (cellStyle == TABLE_CELL_STYLE_FORM) [textField becomeFirstResponder];
    else if (cellStyle == TABLE_CELL_STYLE_NOTE) [textView becomeFirstResponder];
}

-(void)setIsDate:(BOOL)b
{
    isDate = b;
    if (isDate) {
        if (!datePicker) {
            datePicker = [[UIDatePicker alloc] init];
            NSDate *date = [object valueForKey:key];
            if (date) datePicker.date = date;
            datePicker.datePickerMode = (showTime) ? UIDatePickerModeDateAndTime : UIDatePickerModeDate;
            [datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
        }
        textField.inputView = datePicker;
    } else {
        textField.inputView = nil;
    }
}

-(BOOL)isDate
{
    return isDate;
}

-(void)datePickerValueChanged:(id)sender
{
    textField.text = [LOAppDelegate stringFromDate:datePicker.date withTime:showTime];
}

-(BOOL)textFieldShouldReturn:(UITextField *)tf
{
    [tf endEditing:YES];
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)tf
{
    if (!isDate) {
        if (key) [object setValue:tf.text forKey:key];
    } else {
        if (key) [object setValue:datePicker.date forKey:key];
    }
}

- (BOOL)textView:(UITextView *)tv shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
	
    if([text isEqualToString:@"\n"]) {
        [tv resignFirstResponder];
        return NO;
    }
	
    return YES;
}

-(void)textViewDidEndEditing:(UITextView *)tv
{
    if (key) [object setValue:tv.text forKey:key];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
